package com.example.demo1.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.TimePicker
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.example.demo1.R
import com.example.demo1.databinding.ActivityEditInfoBinding
import com.example.utils.onRequestPermissionsResult
import com.example.utils.showBottomSheetDialog
import java.text.SimpleDateFormat
import java.util.Date
/**
 * 编辑信息界面activity
 * **/
class EditInfoActivity : AppCompatActivity() {
    private lateinit var binding: ActivityEditInfoBinding
    private lateinit var pvTime: TimePicker


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityEditInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()

    }

    private fun initView() {
        setSupportActionBar(binding.toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setDisplayShowTitleEnabled(false)
        binding.toolbar.navigationIcon =
            resources.getDrawable(R.drawable.baseline_arrow_back_ios_24)
        val pvTime = TimePickerBuilder(this, object : OnTimeSelectListener {
            override fun onTimeSelect(date: Date?, v: View?) {
                binding.tvBirthdayChoose.text= SimpleDateFormat("yyyy-MM-dd").format(date)
            }
        })  .setCancelText("取消")//取消按钮文字
            .setCancelColor(Color.BLACK)
            .setSubmitText("确定")//确认按钮文字
            .setSubmitColor(Color.BLACK)
            .setContentTextSize(18)//滚轮文字大小
            .setTitleSize(20)//标题文字大小
            .setTitleText("生日")//标题文字
            .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
            .build()
        binding.tvBirthdayChoose.setOnClickListener{
            pvTime.show()
        }
        binding.iconImg.setOnClickListener {
            showBottomSheetDialog(this)
        }

    }


    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        onRequestPermissionsResult(this, requestCode, permissions, grantResults)
        when (requestCode) {
            1 -> {
               if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
                   startActivity(Intent(MediaStore.ACTION_IMAGE_CAPTURE))

               }               }
            2 -> {

                startActivity(
                    Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                    )
                )
            }
    }

}
}